 class ListNode {
    int val;
     ListNode next;
     ListNode() {}
    ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
     ListNode dummyHead=new ListNode(0);
     dummyHead.next=head;
     ListNode pre=dummyHead;
     ListNode end=dummyHead;
     while(end.next!=null){
         for(int i=0;i<k&&end!=null;i++) end=end.next;
         if(end==null)break;
         ListNode start=pre.next;
         ListNode nextTurn=end.next;
         end.next=null;
         pre.next = reverse(start);
         start.next=nextTurn;
         pre=start;
         end=pre;
     }
    return  dummyHead.next;
    }

   ListNode  reverse(ListNode head){
        if(head.next==null) return head;
        ListNode node= reverse(head.next);
        head.next.next=head;
        head.next=null;
        return node;
   }
}